#include <header.h>

int main()
{
    const char * port = "9090";
    const char * ip = "192.168.116.128";

    int client_fd = socket(AF_INET , SOCK_STREAM , 0);
    ERROR_CHECK(client_fd , -1 , "socket");

    struct sockaddr_in clientaddr;
    memset(&clientaddr, 0, sizeof(clientaddr));
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_port = htons(atoi(port));
    clientaddr.sin_addr.s_addr = inet_addr(ip);

    int ret = connect(client_fd , (const struct sockaddr * )&clientaddr ,
                      sizeof(clientaddr));
    ERROR_CHECK(ret , -1 , "connect");

    while(1)
    {
        fd_set set;
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO , &set);
        FD_SET(client_fd , &set);

        select(client_fd + 1 , &set , NULL , NULL , NULL);

        if(FD_ISSET(STDIN_FILENO , &set))
        {
            char buf[1024] = {0};
            int len = read(STDIN_FILENO , buf , sizeof(buf));
            send(client_fd , buf , len , 0);
        }

        if(FD_ISSET(client_fd , &set))
        {
            char buf[1024] = {0};
            int ret = recv(client_fd , buf , sizeof(buf) , 0);

            if(ret == 0)
            {
                printf("对方已断开连接 ！！！ \n");
                break;
            }
            
            buf[ret] = '\0';
            printf("Server : %s" , buf);
        }
    }

    close(client_fd);

    return 0;
}


